import  data  from "./data.js";
import cook from "./cook.js"
import foods from "./foods.js"
import message from "./entrance.js";

function Person(){
    this.food = {},
    this.seat = 0;
    this.time = 0;
    this.money = 0;
    this.foodNumer = 0;//用于记录完成菜的个数
    this.imgNumber = 0;
}



var customer = {
    customers:[],
    src : [
        "./restaurant/customer1.png",
        "./restaurant/customer2.png",
        "./restaurant/customer3.png",
        "./restaurant/customer4.png",
        "./restaurant/customer5.png",
        "./restaurant/customer6.png",
        "./restaurant/customer7.png",
    ],
    enterRestaurant(){
        if(customer.customers.length < 6){
            let minNum = 5; let maxNum = 15;
            function randomNum(minNum,maxNum){ 
                switch(arguments.length){ 
                    case 1: 
                        return parseInt(Math.random()*minNum+1,10); 
                    break; 
                    case 2: 
                        return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
                    break; 
                        default: 
                            return 0; 
                        break; 
                } 
            } 
            let waitTime = randomNum(minNum,maxNum);
            //获取顾客随机进入饭店时间
            setTimeout(() => {
                let waitCustomer = document.querySelector(".waitCustomer");
                let waitCustomeImg = document.querySelector(".waitCustomeImg");
                let customers = document.getElementsByClassName("customers");
                let emptySit = 0;//记录空闲座位数目
                let imgNumber = Math.floor((Math.random()*7)+1);
                waitCustomeImg.src = customer.src[imgNumber];
                let sit = function(){
                    for(let i = 0;i<6;i++){
                        if(customers[i].dataset.sit === "false"){
                            emptySit++;
                            customers[i].dataset.sit = "true";
                            waitCustomer.style.visibility = "hidden";
                            customers[i].childNodes[1].src = customer.src[imgNumber]
                            waitCustomer.removeEventListener("click",sit);
                            customer.order(i,imgNumber);
                            break;
                        }
                    }
                    if(emptySit === 0){
                        let text = "已经没有位置了，请等待其它客人用餐完毕！"
                        message.tips(text);
                    }
                }//sit函数，方便关闭settimeout函数
                waitCustomer.style.visibility = "visible";
                let text = "有客人来了，快点击头像让他进来吧！"
                message.tips(text)
                waitCustomer.addEventListener("click",sit);
            },waitTime * 1000);
        }
    },
    order(seat,imgNumber){
        let menu = document.querySelector(".menu");
        let blackBackground = document.querySelector(".blackBackground");
        let changeMoney = document.querySelector(".menu-customer-text")
        let ok = document.getElementsByClassName("menu-btn")[0];
        let leave = document.getElementsByClassName("menu-btn")[1];
        let orderFoods = document.getElementsByClassName("menu-list-item");
        // 获取菜单上各个元素
        let sum = 0;let cost = 0;let sumCost = 0;
        let myMainFoodNumber = 0;
        //中间变量，用于暂存数据，每次菜单更新时都会清0
        let person = new Person(); 
        //创建一位新顾客

        for(let k = 0;k<orderFoods.length;k++){
            orderFoods[k].childNodes[0].checked = false;
            orderFoods[k].childNodes[0].disabled = false;
            //清除上一位顾客的选项
        }

        person.imgNumber = imgNumber;
        //随机图片

        let menuCustomerHead = document.querySelector(".menu-customer-head");
        menuCustomerHead.src = customer.src[person.imgNumber];
        menu.style.visibility = "visible";
        blackBackground.style.background = "rgba(0, 0, 0, 0.4)";
        blackBackground.style.zIndex = "99";
        //菜单出现

        menu.addEventListener("change", () =>{
            let checkFoods = [];
            let sumTime = 0;
            for(var i = 0 ;i<orderFoods.length;i++){
                if(orderFoods[i].childNodes[0].checked === true){
                    sum += foods.entree.find(ele => ele.index === (i+1)).money;
                    cost += foods.entree.find(ele => ele.index === (i+1)).cost;
                    sumTime += foods.entree.find(ele => ele.index === (i+1)).time;
                    checkFoods.push(foods.entree.find(ele => ele.index === (i+1)))
                    // find方法找到其对象数组,计算成本和金钱
                }
            }
            changeMoney.innerHTML = `顾客正在点菜，已点${sum}元的菜`
            person.time = sumTime
            person.money = sum;
            sumCost = (0 - cost);
            person.food = checkFoods;
            person.seat = seat;

            sumTime = 0;
            cost = 0;
            sum = 0;
            checkFoods = [];
            //清0保证渲染正确

            let coldFood = document.getElementsByClassName("cold-food");
            let mainFood = document.getElementsByClassName("main-food");
            let drink = document.getElementsByClassName("drink");

            let coldFoodNumber = 0;
            let mainFoodNumber = 0;
            let drinkNumber = 0;
            //定义一些符合点菜规则的变量
            for(let i = 0;i<coldFood.length;i++){
                if(coldFood[i].children[0].checked === true){
                    coldFoodNumber++
                }
            }
            for(let i = 0 ;i<coldFood.length;i++){
                if(coldFoodNumber === 1){
                    if(coldFood[i].children[0].checked !== true){
                        coldFood[i].children[0].disabled = true ;
                    }
                }else if(coldFoodNumber === 0){
                    coldFood[i].children[0].disabled = false;
                }
            }
            //凉菜的checkbox控制，三选一或者不选；
            for(let i = 0;i<drink.length;i++){
                if(drink[i].children[0].checked === true){
                    drinkNumber++
                }
            }
            for(let i = 0 ;i<drink.length;i++){
                if(drinkNumber === 1){
                    if(drink[i].children[0].checked !== true){
                        drink[i].children[0].disabled = true ;
                    }
                }else if(drinkNumber === 0){
                    drink[i].children[0].disabled = false;
                }
            }
            //饮料的checkbox控制，三选一或者不选
            for(let i = 0;i<mainFood.length;i++){
                if(mainFood[i].children[0].checked === true){
                    mainFoodNumber++
                }
            }
            for(let i = 0 ;i<mainFood.length;i++){
                if(mainFoodNumber === 1){
                    if(mainFood[i].children[0].checked !== true){
                        mainFood[i].children[0].disabled = true ;
                    }
                }else if(mainFoodNumber === 0){
                    mainFood[i].children[0].disabled = false;
                }
            }
            myMainFoodNumber = mainFoodNumber;
        })

        ok.onclick = () =>{
            if(person.food.length === 0){
                alert("你还没有点任何菜！")
                return 0;
            }
            if(myMainFoodNumber === 0){
                alert("你还没点主菜！")
                return 0;
            }
            data.renderMoney(sumCost)
            menu.style.visibility = "hidden";
            blackBackground.style.background = "";
            blackBackground.style.zIndex = "-99";
            //隐藏菜单
            customer.waiting(person);
            cook.manageCook(person);
            //客人等待，厨师做菜

            customer.enterRestaurant();
        }
        leave.onclick = () =>{
            let waitCustomer = document.querySelector(".waitCustomer");
            let customers = document.getElementsByClassName("customers")
            menu.style.visibility = "hidden";
            blackBackground.style.background = "";
            blackBackground.style.zIndex = "-99";
            //隐藏菜单
            customers[seat].childNodes[1].src = "./restaurant/noPeople.png"
            customers[seat].dataset.sit = "false";
            //清空座位
            customer.enterRestaurant();
            //无论是否点菜成功，下一位客人继续进入
        }
        

    },
    waiting(personData){
        let myFoods = document.getElementsByClassName("wait-foods");
        for(let i = 0 ;i < personData.food.length ; i++){
            myFoods[personData.seat].childNodes[(2 * i + 1)].style.visibility = "visible";
            myFoods[personData.seat].childNodes[(2 * i + 1)].innerHTML = personData.food[i].name;
        }
    },
    checkFoods(number){
        console.log(cook.foods[number]);
        let waitFoods = document.getElementsByClassName("wait-food");
        for(let i = 0;i<waitFoods.length;i++){
            if(waitFoods[i].innerHTML === cook.foods[number].food.name && waitFoods[i].dataset.ready === 'false'){
                waitFoods[i].dataset.ready = "true";
                waitFoods[i].style.textDecorationLine = "line-through";
                cook.foods[number].person.foodNumer++
                if(cook.foods[number].person.foodNumer === cook.foods[number].person.food.length){
                    customer.eating(number);
                    //判断菜上齐没有
                }
                break;
            }
        }
    },
    eating(number){
        let text = "一位客人的所有菜品已经上齐"
        message.tips(text)
        setTimeout(() => {
            customer.leaveRestaurant(number)
        }, (cook.foods[number].person.time) * 1000);
    },
    leaveRestaurant(number){
        let customer = document.getElementsByClassName("customers")[cook.foods[number].person.seat];
        //此处顾客的字节的1 3 5 分别对应图片，菜单和状态栏
        customer.childNodes[1].src = "./restaurant/noPeople.png";
        customer.dataset.sit = "false";
        for(let i = 0 ;i<customer.childNodes[3].children.length;i++){
            customer.childNodes[3].children[i].innerHTML = "";
            customer.childNodes[3].children[i].style.textDecorationLine = "none"
            customer.childNodes[3].children[i].style.visibility = "hidden";
            customer.childNodes[3].children[i].dataset.ready = false;
        }
        data.renderMoney(cook.foods[number].person.money);
    }
}

export default customer;